package com.zy.tools.appletsTools;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间转换工具类
 */
@Component
public class TimeTool {

    /**
     * 将时间戳转换为 yyyy-MM-dd HH:mm:ss
     *
     * @param timeStamp
     * @return
     */
    public String stampToDate(String timeStamp) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(timeStamp);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间yyyy-MM-dd HH:mm:ss转换为时间戳
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public String dateToStamp(String time) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(time);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /**
     * 将时间yyyy-MM-dd HH:mm:ss转换为时间戳
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public long dateToStampLong(String time) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(time);
        long ts = date.getTime();
        return ts;
    }

    /**
     * 将时间yyyy-MM-dd转换为时间戳
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public long dateToStampLongOther(String time) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(time);
        long ts = date.getTime();
        return ts;
    }
}
